import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;

public class LogIn extends JDialog {



    public LogIn(JFrame frame){
        super(frame,"登录",true);
        setBounds(720,405,150,250);
        Container c = getContentPane();
        c.add(new JLabel("用户名   "));
        JTextField username = new JTextField(10);
        c.add(username);
        c.add(new JLabel("密码     "));
        JPasswordField password = new JPasswordField(10);
        c.add(password);
        JButton login_1 = new JButton("登录");
        c.add(login_1);
        c.setLayout(new FlowLayout());
        c.add(new JLabel("     重置密码    "));
        JPasswordField passwordReborn = new JPasswordField(10);
        c.add(passwordReborn);
        JButton confirm = new JButton("确认");
        c.add(confirm);

        login_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag = false;
                String name = username.getText();
                String pwd = new String(password.getPassword());

                for(Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()){
                    String key = entry.getKey();
                    List<String> values = User.getUserMap().get(key);
                    if(key.equals(name) && values.get(0).equals("123456")){
                        flag = true;
                            JOptionPane.showMessageDialog(null,"登录失败，您的密码为默认密码,请修改密码\n" +
                                    "（在重置密码中输入新密码后点击确认以重置密码）");
                        break;
                    }
                    else if(key.equals(name) && values.get(0).equals(pwd)){
                        flag = true;
                        User.nowUser = name;
                        JOptionPane.showMessageDialog(null,"登录成功,您的身份是"+values.get(1));
                        dispose();
                        break;
                    }

                }
                if(User.nowUser == null && !flag){
                    JOptionPane.showMessageDialog(null,"登录失败,请检查用户名和密码是否正确");
                }
            }

        });

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String name = username.getText();
                String newpPwd = new String(passwordReborn.getPassword());

                for (Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()) {
                    String key = entry.getKey();
                    List<String> values = User.getUserMap().get(key);
                    if (key.equals(name) && values.get(0).equals("123456")) {
                        User.setUserMap(key,newpPwd,values.get(1),values.get(2),values.get(3));
                        JOptionPane.showMessageDialog(null,"密码修改成功");
                        break;
                    }
                }
            }
        });

    }
}